# Maintainer: Yusuke Sasaki <yusuke.sasaki.nuem@gmail.com>

_realname=cppcheck
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.1
pkgrel=1
pkgdesc="static analysis of C/C++ code (mingw-w64)"
arch=('any')
url="https://cppcheck.sourceforge.io/"
license=('GPL3')
depends=("${MINGW_PACKAGE_PREFIX}-pcre")
makedepends=("${MINGW_PACKAGE_PREFIX}-docbook-xsl"
             "${MINGW_PACKAGE_PREFIX}-qt5"
             "${MINGW_PACKAGE_PREFIX}-python")
optdepends=("${MINGW_PACKAGE_PREFIX}-qt5: cppcheck-gui"
            "${MINGW_PACKAGE_PREFIX}-python-pygments: cppcheck-htmlreport")
source=(${_realname}-${pkgver}.tar.gz::"https://github.com/danmar/cppcheck/archive/${pkgver}.tar.gz"
        '0010-change-language-files-path.patch'
        '0020-change-cfg-path.patch')
sha256sums=('447d44bbaa555fa78b89dd2cb0203fd4c6f18269db8a78638b968ba7c72cb02e'
            '574b040179c29a3eac19e7abd121f5d9c13c72a5dfdb66f4d903357da1e3944d'
            '3d54eb5abb99f88846e0d2a1e21793904efbe7179464631ca05985d03642d134')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -p1 -i "${srcdir}/0010-change-language-files-path.patch"
  patch -p1 -i "${srcdir}/0020-change-cfg-path.patch"
}

build() {
  [[ -d "${srcdir}"/build-${CARCH} ]] && rm -rf "${srcdir}"/build-${CARCH}
  cp -r "${srcdir}/${_realname}-${pkgver}" "${srcdir}"/build-${CARCH} && cd "${srcdir}"/build-${CARCH}
  
  LANG='en_US.UTF-8' make \
    PREFIX=${MINGW_PREFIX} \
    CXX=${MINGW_PREFIX}/bin/g++ \
    LD=${MINGW_PREFIX}/bin/ld \
    MATCHCOMPILER=yes \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck \
    HAVE_RULES=yes \
    LDFLAGS=-lshlwapi RDYNAMIC=

  make man \
    DB2MAN=${MINGW_PREFIX}/share/xml/docbook/xsl-stylesheets-1.79.2/manpages/docbook.xsl \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck

  cd gui
  lrelease gui.pro
  ${MINGW_PREFIX}/bin/qmake HAVE_RULES=yes HAVE_QCHART=yes CONFIG+=release
  make \
    CXX=${MINGW_PREFIX}/bin/g++ \
    LD=${MINGW_PREFIX}/bin/ld \
    PREFIX=${MINGW_PREFIX} \
    MATCHCOMPILER=yes \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck \
    HAVE_RULES=yes \
    HAVE_QCHART=yes
}

check() {
  cd "${srcdir}"/build-${CARCH}
   LANG='en_US.UTF-8' make test \
     MATCHCOMPILER=yes \
     CXX=${MINGW_PREFIX}/bin/g++ \
     LD=${MINGW_PREFIX}/bin/ld \
     PREFIX="${MINGW_PREFIX}" \
     FILESDIR=${MINGW_PREFIX}/share/cppcheck \
     HAVE_RULES=yes \
     HAVE_QCHART=yes \
     LDFLAGS=-lshlwapi RDYNAMIC=
}

package() {
  cd "${srcdir}"/build-${CARCH}

  LANG='en_US.UTF-8' make install \
    DESTDIR="${pkgdir}" \
    CXX=${MINGW_PREFIX}/bin/g++ \
    LD=${MINGW_PREFIX}/bin/ld \
    PREFIX="${MINGW_PREFIX}" \
    MATCHCOMPILER=yes \
    FILESDIR=${MINGW_PREFIX}/share/cppcheck \
    HAVE_QCHART=yes \
    HAVE_RULES=yes

  # man and COPYING
  install -Dpm644 cppcheck.1 "${pkgdir}${MINGW_PREFIX}/share/man/man1/cppcheck.1"
  install -Dpm644 COPYING    "${pkgdir}${MINGW_PREFIX}/share/licenses/cppcheck/COPYING"

  # GUI
  install -m755 build/gui/cppcheck-gui.exe "${pkgdir}${MINGW_PREFIX}/bin"
  install -d "${pkgdir}${MINGW_PREFIX}/share/cppcheck/cfg"
  install -d "${pkgdir}${MINGW_PREFIX}/share/cppcheck/lang"
  install -D cfg/* -t "${pkgdir}${MINGW_PREFIX}/share/cppcheck/cfg"
  install -D gui/*.qm -t "${pkgdir}${MINGW_PREFIX}/share/cppcheck/lang"
}
